<template>
  <div class="app-container">
    <h3>医院设置列表</h3>

    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="医院编号">
        <el-input v-model="searchObj.hoscode" placeholder="医院名称"></el-input>
      </el-form-item>

      <el-form-item label="医院名称">
        <el-input
          v-model="searchObj.hosname"
          placeholder="医院名称  - 模糊查询"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getList()">查询</el-button>
      </el-form-item>
    </el-form>

    <div>
      <el-button type="danger" size="mini" @click="batchRemoveHospitalSet"
        >批量删除</el-button
      >
    </div>

    <el-table
      :data="list"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"></el-table-column>

      <el-table-column prop="id" label="序号" width="60"> </el-table-column>
      <el-table-column prop="hosname" label="医院名称" width="180">
      </el-table-column>
      <el-table-column prop="hoscode" label="医院编号" width="180">
      </el-table-column>
      <el-table-column prop="apiUrl" label="api基本路径" width="180">
      </el-table-column>
      <el-table-column prop="contactsName" label="联系人姓名" width="180">
      </el-table-column>
      <el-table-column prop="contactsPhone" label="联系人手机" width="180">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="180">
        <template slot-scope="scope">
          <el-tag   :type="scope.row.status === 1 ? 'primary' : 'success'" disable-transitions>{{ scope.row.status == 1 ? "可用" : "不可用" }}</el-tag>
        </template>
      </el-table-column>
      <!-- 
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column> -->

      <el-table-column width="300"  label="操作">
        <template  slot-scope="scope">
          <el-button size="mini" v-if="scope.row.status==1"  type="primary"  @click="lockHospSet(scope.row.id,0)">锁定</el-button>
          <el-button size="mini" v-else  type="danger"  @click="lockHospSet(scope.row.id,1)">取消锁定</el-button>
          <el-button size="mini" icon="el-icon-delete"   type="danger"  @click="removeDataById(scope.$index, scope.row)">删除</el-button>
         
           <!-- 页面路由跳转 并且穿参数 -->
          <router-link :to="'/hospSet/edit/' + scope.row.id">
          <el-button size="mini" type="primary" icon="el-icon-edit">修改</el-button>
          </router-link>

          
         
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      @current-change="getList"
      background
      style="padding: 30px 0; text-align: center"
    />
  </div>
</template>

<script>
// 引入接口定义js文件
import hospset from "@/api/hospset";

export default {
  data() {
    return {
      current: 1, // 当前页
      limit: 3, //每页显示记录数
      searchObj: {}, // 条件封装对象
      list: [], // 每页数据集合
      total: 0, // 总记录数
      multipleSelection: [], // 多个Id值
    };
  },
  created() {
    // 页面渲染之前执行
    // 一般调用method定义的方法，得到数据
    this.getList();
  },
  methods: {
    // 锁定和取消锁定
    lockHospSet(id,status){
      hospset.lockHospSet(id,status)
        .then(response => {
          // 刷新
          this.getList(this.current)
        });
    },




    // 定义方法，接口请求调用
    // 医院设置列表
    getList(page = 1) {
      this.current = page; // 添加当前页的参数
      hospset
        .getHospSetList(this.current, this.limit, this.searchObj)
        .then((response) => {
          // console.log(response);
          // 请求成功之后 Response是接口返回数据
          // 返回集合并且赋值
          this.list = response.data.records;
          // 总记录数
          this.total = response.data.total;
          //   console.log(this.list);
        })
        .catch((error) => {
          // 请求失败调用
          console.log(error);
        });
    },

    // 删除医院设置的方法
    removeDataById(index, row) {
      // console.log(index, row);
      this.$confirm("此操作将永久删除该医院信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 确定执行
          // 调用接口
          hospset.removeDataById(row.id).then((response) => {
            // 提示
            this.$message({
              type: "success",
              message: "删除成功!",
            });

            // 刷新页面
            this.getList(1);
          });
        })
        .catch(() => {
          // 取消执行
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取复选框里面的值
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    // 批量删除
    batchRemoveHospitalSet() {
      this.$confirm("此操作将永久删除该医院信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const idList = [];
          // 便利每个Id值，设置的idList里面
          for(var i = 0; i < this.multipleSelection.length; i++){
            const item = this.multipleSelection[i];
            idList.push(item.id);
          }
          // 确定执行
          // 调用接口
          hospset.batchRemoveHospitalSet(idList).then((response) => {
            // 提示
            this.$message({
              type: "success",
              message: "删除成功!",
            });

            // 刷新页面
            this.getList(1);
          });
        })
        .catch(() => {
          // 取消执行
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
</style>